package com.ruoyi.app.W.controller;

import com.ruoyi.app.W.domain.advice;
import com.ruoyi.app.W.domain.vo.photoVo;
import com.ruoyi.app.W.mapper.WMapper;
import com.ruoyi.app.dyh.hxf.domain.Message;
import com.ruoyi.app.dyh.hxf.service.IMessageService;
import com.ruoyi.app.W.service.WService;
import com.ruoyi.app.zzq.domain.Partydues;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysNotice;
import com.ruoyi.project.system.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.utils.ParameterizedTypeImpl.parseString2List;


@RestController
@RequestMapping("/api/home")
public class WController {
    @Autowired
    WService WService;
    @Autowired
    WMapper wMapper;
    @Autowired
    IMessageService iMessageService;

    /**
     轮播图
     * @return
     */
    @RequestMapping(value = "/SelectCarousele",method = RequestMethod.GET)
    public AjaxResult getCarousel(Integer userId) {
        List<SysNotice> notices= WService.SelectCarousele(userId);
//        System.out.println(userId+"222");
        System.out.println(notices+"///");
        for (SysNotice notice : notices) {
            List<photoVo> list = parseString2List(notice.getNotice_photo(), photoVo.class);
            List<String> photoList = list.stream().map(photoVo::getUrl).collect(Collectors.toList());
            notice.setNotice_photo(StringUtils.strip(photoList.toString().replaceAll(" ",""),"[]"));
        }
        return AjaxResult.success(notices);
    }
    /**
     通知
     * @return
     */
    @RequestMapping(value = "/SelectCurrNotice",method = RequestMethod.GET)
    public AjaxResult SelectCurrNotice(Integer userId) {
        List<SysNotice> notices= WService.SelectCurrNotice(userId);
        System.out.println(notices.get(0).getCreateBy()+"22222");
        for (SysNotice notice : notices) {
            List<photoVo> list = parseString2List(notice.getNotice_photo(), photoVo.class);
            List<String> photoList = list.stream().map(photoVo::getUrl).collect(Collectors.toList());
            notice.setNotice_photo(StringUtils.strip(photoList.toString().replaceAll(" ",""),"[]"));
        }
        System.out.println("photoList"+notices);
        return AjaxResult.success(notices);
    }
    /** 通知页详情
     * @param noticeId
     * @return
     */
    @RequestMapping(value = "/SelectNotices/{noticeId}",method = RequestMethod.GET)
    public List<SysNotice> Notices(@PathVariable("noticeId") String noticeId) {
        List<SysNotice> notices= WService.notices(noticeId);
        return notices;
    }
    /**
     * 查看用户谏言能够提交的层级,每人查看的层级不同
     */

    @RequestMapping(value = "/showAdviceHierarchy",method = RequestMethod.PUT)
    public AjaxResult  showAdviceHierarchy( @RequestParam(value="userId")  String userId) {
        Map<String, String> AdviceHierarchy = WService.showAdviceHierarchy("1");
        return AjaxResult.success(AdviceHierarchy);
    }

    /**
     * 提交谏言，自由选择提交的等级   SubmitAdvice Post
     */
    @RequestMapping(value = "/SubmitAdvice",method = RequestMethod.POST)
    public AjaxResult SubmitAdvice(advice advice) {
        Message message = new Message();
        advice.setAdviceDelete(0);
        advice.setAdviceTime(DateUtils.getNowDate());
        if(WService.SubmitAdvice(advice)){
            message.setMsgTitle(advice.getAdviceTitle());
            message.setMsgContent(advice.getAdviceContent());
            message.setMgsStatus(2);
            message.setCreateTime(advice.getAdviceTime());
            message.setReceiverId("1");
            message.setRemark(advice.getRemark());
            iMessageService.insertMessage(message);
            return AjaxResult.success("谏言提交成功");
        }
        return AjaxResult.error("谏言提交失败！！");
    }
    //查看谏言
    // 1：查看我的谏言
    @RequestMapping(value = "/SelectmyAdvice",method = RequestMethod.GET)
    public AjaxResult SelectmyAdvice(String launchPartyId){
        List<advice> advice=WService.SelectmyAdvice(launchPartyId);
        return AjaxResult.success(advice);
    }
    // 2：查看所有谏言功能
    @RequestMapping(value = "/SelectAllAdvice",method = RequestMethod.GET)
    public AjaxResult SelectAllAdvice(){
        List<advice> advice=WService.SelectAllAdvice();
        return AjaxResult.success(advice);
    }
    //修改未回复的谏言(前台判断是否回复)
    @RequestMapping(value = "/UpdatemyAdvice",method = RequestMethod.PUT)
    public AjaxResult UpdatemyAdvice(advice advice){
        System.out.println(advice+"@@@");
        Message message = new Message();
        if( WService.UpdatemyAdvice(advice)){
            message.setMsgTitle(advice.getAdviceTitle());
            message.setMsgContent(advice.getAdviceContent());
            message.setMgsStatus(2);
            message.setCreateTime(advice.getAdviceTime());
            message.setReceiverId("1");
            message.setRemark(advice.getRemark());
            iMessageService.insertMessage(message);
            return AjaxResult.success("谏言修改成功");
        }
        return AjaxResult.error("谏言修改失败！！");
    }
    //撤回未回复的谏言
    @RequestMapping(value = "/DeletemyAdvice",method = RequestMethod.PUT)
    public AjaxResult DeletemyAdvice( Integer adviceId){
        advice advice= wMapper.selectActive(adviceId);
        Message message = new Message();
        message.setMsgTitle(advice.getAdviceTitle());
        message.setMsgContent(advice.getAdviceContent());
        message.setMgsStatus(2);
        message.setCreateTime(advice.getAdviceTime());
        message.setReceiverId(advice.getLaunchPartyId());
        message.setRemark(advice.getRemark());
        iMessageService.insertMessage(message);
        if( WService.DeletemyAdvice(adviceId)){
            return AjaxResult.success("撤回成功");
        }
        return AjaxResult.error("撤回失败！！");
    }
    //党费缴纳模块    GET
    //查询是否已缴纳党费
    @RequestMapping(value = "/SelectIfPays",method = RequestMethod.GET)
    public AjaxResult SelectIfPays(Integer userId){
        List<Partydues> partydues=wMapper.selectPartyDues(userId);

        if(partydues.size()!=0){
            return AjaxResult.success("未缴纳！！",1);

        }
        return AjaxResult.success("已缴纳",0);

    }
    //公告通知模块
    //查看本社区的公告 GET  SelectMyComNotice
    @RequestMapping(value = "/SelectMyComNotice/{userId}",method = RequestMethod.GET)
    public AjaxResult SelectMyComNotice(@PathVariable("userId") String userId) {
        List<SysNotice> notices= WService.SelectMyComNotice(wMapper.selectCommunityId(userId));
        return AjaxResult.success(notices);
    }

    //getUserid
    @GetMapping("getUser")
    public AjaxResult getUserid()
    {
        SysUser sysUser=SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success(sysUser);
    }

    @RequestMapping(value = "/MyPayTydue",method = RequestMethod.GET)
    public AjaxResult MyPayTydue(String userId){
        Date payTime =DateUtils.getNowDate();
        boolean i=WService.MyPayTydue(payTime, Integer.valueOf(userId));
        if(i){
            Message message = new Message();
            message.setMsgTitle("党费缴费");
            message.setMsgContent("党费缴纳成功");
            message.setMgsStatus(2);
            message.setReceiverId(userId);
            message.setCreateTime(payTime);
            iMessageService.insertMessage(message);
        }
        return AjaxResult.success(i);
    }
}
